<?php

namespace apiend\modules\dynamic\controllers;

use apiend\modules\dynamic\models\DynamicSearchForDetail;
use Yii;
use apiend\components\Controller;
use common\models\dynamic\Dynamic;
use common\models\dynamic\DynamicComment;

/**
 * 动态&评论相关管理类功能接口
 * Default controller for the `dynamic` module
 */
class ManagerController extends Controller {

    /**
     * @inheritdoc
     */
    protected function rules() {
        return [
            [
                'allow' => true,
                'roles' => ['@'],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    protected function verbs() {
        return [
            'delete' => ['DELETE'],
            'dynamicmess'=>['GET']
        ];
    }

    /**
     * 删除动态&评论
     * @param $dynamic_id 动态ID（必传）
     * @param $comment_id 评论ID（如删除动态评论此项必填）
     * @return boolean
     */
    public function actionDelete($dynamic_id, $comment_id = 0) {
        if (!$comment_id) {
            $model = Dynamic::findOrFail($dynamic_id);
            if ($model->user_id == $this->token->user_id) {
                return $model->delete();
            }
            return false;
        }
        $model = DynamicComment::findx($dynamic_id)->where(['id' => $comment_id])->one();
        if ($model && $model->user_id == $this->token->user_id) {
            return $model->delete();
        }
        return false;
    }

    public function actionDetail($id){
        $model = new DynamicSearchForDetail();
        return $model->search($id);
    }

}
